package com.flower.enjoyment.flower.service.impl;

import com.flower.enjoyment.flower.entity.FlowerCardOrderDetail;
import com.flower.enjoyment.flower.entity.OrderInfo;
import com.flower.enjoyment.flower.entity.PresentDetail;
import com.flower.enjoyment.flower.entity.UserInfo;
import com.flower.enjoyment.flower.entityDo.PresentDetailDo;
import com.flower.enjoyment.flower.entityDo.ShopInfoDo;
import com.flower.enjoyment.flower.entityDo.SysBannerDo;
import com.flower.enjoyment.flower.entityDo.UserInfoDo;
import com.flower.enjoyment.flower.mapper.*;
import com.flower.enjoyment.flower.service.UserInfoService;
import com.flower.enjoyment.flower.utils.RandomUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class UserServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private FlowerCardOrderDetailMapper flowerCardOrderDetailMapper;
    @Autowired
    private PresentDetailMapper presentDetailMapper;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    SysBannerMapper sysBannerMapper;


    /**
     * 根据openID获取用户信息
     *
     * @param openId
     * @return
     */
    @Override
    public UserInfoDo selectByOpenId(String openId) {
        return userInfoMapper.selectByOpenId(openId);
    }

    /**
     * 根据id获取用户信息
     *
     * @param id
     * @return
     */
    @Override
    public UserInfoDo selectById(String id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * 更新用户信息
     *
     * @param user
     * @return
     */
    @Override
    @Transactional
    public int updateById(UserInfo user) {
        return userInfoMapper.updateByPrimaryKeySelective(user);
    }

    /**
     * 添加用户信息
     *
     * @param user
     * @return
     */
    @Override
    @Transactional
    public int insert(UserInfo user) {
        if (StringUtils.isNotBlank(user.getSourceUserId())){
            //获取赠送信息
            SysBannerDo bannerDo = sysBannerMapper.getShareInfo();
            if (bannerDo!=null){
                UserInfo userInfo = new UserInfo();
                UserInfoDo infoDo = userInfoMapper.selectByPrimaryKey(user.getSourceUserId());
                //计算时间

                userInfo.setId(user.getSourceUserId());
                userInfo.setShareUserAmount(infoDo.getShareUserAmount()+bannerDo.getAmount());
                userInfo.setTotalIncome(infoDo.getTotalIncome()+bannerDo.getAmount());
                userInfo.setAmount(infoDo.getAmount()+bannerDo.getAmount());
                userInfoMapper.updateShareAmount(userInfo);
            }
        }
        return userInfoMapper.insert(user);
    }

    /**
     * 支付后跟新用户信息及店主收入
     *
     * @param userInfoDo
     * @param orderInfo
     * @return
     */
    @Override
    @Transactional
    public int updateUserOrderInfo(UserInfoDo userInfoDo, OrderInfo orderInfo, PresentDetailDo presentDetailDo) {
        //double giveMoney = 0.0;

        UserInfo userInfo = new UserInfo();
        userInfo.setId(orderInfo.getUserId());
        FlowerCardOrderDetail flowerCardOrderDetail = new FlowerCardOrderDetail();
        flowerCardOrderDetail.setId(RandomUtil.getUuid());
        flowerCardOrderDetail.setCreateTime(new Date());
        flowerCardOrderDetail.setServiceName(orderInfo.getServiceName());
        flowerCardOrderDetail.setUserId(orderInfo.getUserId());

        double userCountAmount = (userInfo.getAmount()+userInfo.getGiveAmount()) - orderInfo.getActualAmount();
        //用户的金额+用户的赠送金额 大于当前需支付的金额
        if (userCountAmount > 0){
            PresentDetail presentDetail = new PresentDetail();
            presentDetail.setId(RandomUtil.getUuid());
            presentDetail.setUserId(orderInfo.getUserId());
            presentDetail.setServiceName(orderInfo.getServiceName());
            presentDetail.setCreateTime(new Date());

            if (userInfo.getGiveAmount()>=orderInfo.getActualAmount()){
                //用户赠送金额大于订单金额
                presentDetail.setBalance(presentDetailDo.getBalance() - orderInfo.getActualAmount());
                presentDetail.setAmount(orderInfo.getActualAmount());

                presentDetailMapper.insert(presentDetail);
            }else{
                //用卡支付时将卡支付信息记录
                double shengyu =0.0;
                if (userInfo.getGiveAmount() > 0){
                    shengyu = orderInfo.getActualAmount()-userInfo.getGiveAmount();
                    userInfo.setGiveAmount(0.0);

                    presentDetail.setAmount(userInfo.getGiveAmount());
                    presentDetail.setBalance(0.0);
                    presentDetailMapper.insert(presentDetail);
                }
                userInfo.setAmount(userInfo.getAmount()-shengyu);
                flowerCardOrderDetail.setAmount(shengyu);
                flowerCardOrderDetail.setBalance(userInfo.getAmount());
                flowerCardOrderDetailMapper.insert(flowerCardOrderDetail);
            }
        }
       /* double cardConsumeAmount = userInfo.getAmount() - orderInfo.getActualAmount();//用户卡金额减去订单金额是否还剩余
        if (cardConsumeAmount < 0) {
            giveMoney = userInfo.getGiveAmount() + cardConsumeAmount;

            if (userInfo.getAmount() > 0) {
                flowerCardOrderDetail.setAmount(orderInfo.getActualAmount());
                flowerCardOrderDetail.setBalance(userInfo.getAmount());
                flowerCardOrderDetailMapper.insert(flowerCardOrderDetail);
            }
            PresentDetail presentDetail = new PresentDetail();
            presentDetail.setId(RandomUtil.getUuid());
            presentDetail.setUserId(orderInfo.getUserId());
            presentDetail.setServiceName(orderInfo.getServiceName());
            presentDetail.setCreateTime(new Date());
            presentDetail.setAmount(userInfo.getGiveAmount() - giveMoney);
            presentDetail.setBalance(presentDetail.getBalance() - giveMoney);
            presentDetailMapper.insert(presentDetail);
            userInfo.setGiveAmount(giveMoney);
            userInfo.setAmount(0.0);

        } else {
            userInfo.setAmount(cardConsumeAmount);
            flowerCardOrderDetail.setAmount(orderInfo.getActualAmount());
            flowerCardOrderDetail.setBalance(userInfo.getAmount());
            flowerCardOrderDetailMapper.insert(flowerCardOrderDetail);
        }*/
        userInfoMapper.updateByPrimaryKeySelective(userInfo);
        orderInfoMapper.insert(orderInfo);
        ShopInfoDo shopInfoDo = shopMapper.selectByShopId(orderInfo.getShopId());
        UserInfoDo bossInfoDo = userInfoMapper.selectByPrimaryKey(shopInfoDo.getUserId());
        UserInfo bossInfo = new UserInfo();
        bossInfo.setId(bossInfoDo.getId());
        bossInfo.setIncomeBalance(bossInfoDo.getIncomeBalance() + orderInfo.getActualAmount());
        bossInfo.setTodayIncome(bossInfoDo.getTodayIncome() + orderInfo.getActualAmount());
        bossInfo.setTotalIncome(bossInfoDo.getTotalIncome() + orderInfo.getActualAmount());
        userInfoMapper.updateByPrimaryKeySelective(bossInfo);

        //修改卡中赠送余额的数量

        return 0;
    }

    @Override
    public List<UserInfoDo> selectListBySourceUserId(String sourceUserId) {
        return userInfoMapper.selectListBySourceUserId(sourceUserId);
    }

    @Override
    public int updateTodayIncome() {
        return userInfoMapper.updateTodayIncome();
    }


}
